package panel.viewSpots.navigationNew
{
	import assists.HtmlText;
	import assists.TextFieldUtils;
	import assists.resource.ResourceLoader;
	
	import com.hurlant.crypto.symmetric.NullPad;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;
	
	public class NavigationSearchPanel extends Sprite
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		private var _searchBtn:Sprite = new Sprite();
		private var _searchField:Sprite = new Sprite();
		private var _searchTxt:TextField = new TextField();
		private var _aboutContainer:Sprite = new Sprite();
		private var _onOpenDetail:Function;
		private var _boxList:Array = [];
		public function NavigationSearchPanel(stage:Stage,loader:ResourceLoader)
		{
			_loader = loader;
			_stage = stage;
			init();
		}
		
		private function init():void
		{
			this.graphics.beginFill(0xffffff);
			this.graphics.drawRect(0,0,_stage.stageWidth,_stage.stageHeight);
			this.graphics.endFill();
			
			_searchBtn.addChild(new _loader.uiManager.searchPng());
			_searchBtn.y = _stage.stageHeight * 0.02;
//			_searchBtn.width = _searchBtn.height = _stage.stageWidth * 0.15;
			_searchBtn.addEventListener(MouseEvent.CLICK,clickSearchHandler);
			
			_searchField.addChild(new _loader.uiManager.searchFieldPng());
			_searchField.width = _stage.stageWidth * 0.7;
			_searchField.y = _searchBtn.y;
			_searchField.x = _stage.stageWidth * 0.02;
			_searchBtn.x = _searchField.x + _searchField.width + _searchField.width * 0.05;
			
			_searchTxt.multiline = false;
			_searchTxt.autoSize = "left";
			_searchTxt.type = TextFieldType.INPUT;
			_searchTxt.x = _searchField.x + 12;
			
			_searchTxt.text = "输入";
			_searchTxt.y = _searchField.y + (_searchField.height - _searchTxt.height) * 0.5;
			
			var txtFormat:TextFormat = new TextFormat();
			txtFormat.color = 0;
			txtFormat.size = 20 + HtmlText.NUMBER_FONT_CHANGE;
			_searchTxt.defaultTextFormat = txtFormat;
			
			_aboutContainer.y = _searchField.y + _searchField.height * 1.4;
			this.addChild(_searchField);
			this.addChild(_searchBtn);
			this.addChild(_searchTxt);
		    this.addChild(_aboutContainer);	
		
		}
		
		protected function closeHandler(event:KeyboardEvent):void
		{
			if(event.keyCode == Keyboard.BACK)
			{
				close();
				event.stopPropagation();
			}
		}
		
		protected function clickSearchHandler(event:MouseEvent):void
		{
			trace("search" + _searchTxt.text);
			var box:ItemLineBox;
			for(var i:int=0;i<_boxList.length;i++)
			{
				box = _boxList[i];
				if(box.data.name == _searchTxt.text)
				{
					_onOpenDetail(box.data);
					close();
					break;
				}
			}
		}
		
		public function show(dataList:Array,defaultData:Object):void
		{
			_searchTxt.text = defaultData.name;
			for(var i:int=0;i<dataList.length;i++)
			{
				var box:ItemLineBox = new ItemLineBox(_stage,_loader);
				box.y = i * box.height;
				box.show(dataList[i]);
				box.addEventListener(MouseEvent.CLICK,clickBoxHandler);
				_aboutContainer.addChild(box);
				_boxList.push(box);
			   
			}
			_stage.addEventListener(KeyboardEvent.KEY_DOWN,closeHandler);
		}
		
		protected function clickBoxHandler(event:MouseEvent):void
		{
			trace("box");
			var box:ItemLineBox = event.currentTarget as ItemLineBox;
			_onOpenDetail(box.data);
			close();
		}
		
		public function onOpenDetail(value:Function):void
		{
			_onOpenDetail = value;
		}
		
		private function close():void
		{
			var box:ItemLineBox;
			for(var i:int=0;i<_boxList.length;i++)
			{
				box = _boxList[i];
				box.removeEventListener(MouseEvent.CLICK,clickBoxHandler);
				box.parent.removeChild(box);
				box = null;
			}
			_boxList = [];
			_stage.removeEventListener(KeyboardEvent.KEY_DOWN,closeHandler);
			if(this.parent)
			{
				this.parent.removeChild(this);
			}
			
			
		}
		
	}
}